home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_2 / fifolib / lib.c < prev    next >
C/C++ Source or Header  |  1991-11-09  |  4KB  |  157 lines

  1.  
  2. /*
  3.  *  LIB.C
  4.  *
  5.  *  Basic Library Resource Handling
  6.  *
  7.  *  NOTE: all data declarations should be initialized since we skip
  8.  *      normal C startup code (unless initial value is don't care)
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. Prototype __geta4 __stkargs long LibExpunge(long, Library *);
  14.  
  15.  
  16. extern long ALibExpunge(), ALibClose(), ALibOpen();
  17. extern long AOpenFifo(), ACloseFifo(), AReadFifo(), AWriteFifo();
  18. extern long ARequestFifo(), ABufSizeFifo();
  19.  
  20. Library *LibBase = NULL;    /*  Library Base pointer    */
  21. long    SegList  = 0;
  22. ExecBase *SysBase  = NULL;
  23. List    FifoList;
  24. /* Iot       Iot0;           /*  timer request           */
  25.  
  26. /*
  27.  *    The Initialization routine is given only a seglist pointer.  Since
  28.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  29.  *    and return either NULL or the library pointer.  Exec has Forbid()
  30.  *    for us during the call.
  31.  *
  32.  *    If you have an extended library structure you must specify the size
  33.  *    of the extended structure in MakeLibrary().
  34.  */
  35.  
  36. __geta4 __stkargs Library *
  37. LibInit(segment)
  38. long segment;
  39. {
  40.     Library *lib;
  41.     static const long (*Vectors[])() = {
  42.     ALibOpen,
  43.     ALibClose,
  44.     ALibExpunge,
  45.     NULL,
  46.     AOpenFifo,
  47.     ACloseFifo,
  48.     AReadFifo,
  49.     AWriteFifo,
  50.     ARequestFifo,
  51.     ABufSizeFifo,
  52.     (long (*)())-1
  53.     };
  54.  
  55.     SysBase = *(ExecBase **)4;
  56.  
  57. #ifdef NOTDEF
  58.     Iot0.tr_node.io_Device = NULL;
  59.     if (OpenDevice("timer.device", UNIT_MICROHZ, &Iot0, 0))
  60.     return(NULL);
  61.     Iot0.tr_node.io_Command = TR_ADDREQUEST;
  62.     Iot0.tr_time.tv_secs = 0;
  63.     Iot0.tr_time.tv_micro= 0;
  64. #endif
  65.  
  66.     LibBase = lib = MakeLibrary(Vectors,NULL,NULL,sizeof(Library),NULL);
  67.     lib->lib_Node.ln_Type = NT_LIBRARY;
  68.     lib->lib_Node.ln_Name = LibName;
  69.     lib->lib_Flags = LIBF_CHANGED|LIBF_SUMUSED;
  70.     lib->lib_Version  = 37;
  71.     lib->lib_Revision = 3;
  72.     lib->lib_IdString = (APTR)LibId;
  73.     SegList = segment;
  74.     AddLibrary(lib);
  75.  
  76.     NewList((MaxList *)&FifoList);
  77.  
  78.     return(lib);
  79. }
  80.  
  81. /*
  82.  *    Open is given the library pointer and the version request.  Either
  83.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  84.  *    Exec has Forbid() for us during the call.
  85.  */
  86.  
  87. __geta4 __stkargs Library *
  88. LibOpen(version, lib)
  89. Library *lib;
  90. long version;
  91. {
  92.     ++lib->lib_OpenCnt;
  93.     lib->lib_Flags &= ~LIBF_DELEXP;
  94.     return(lib);
  95. }
  96.  
  97. /*
  98.  *    Close is given the library pointer and the version request.  Be sure
  99.  *    not to decrement the open count if already zero.    If the open count
  100.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  101.  *    and return the seglist.  Otherwise we return NULL.
  102.  *
  103.  *    Note that this routine never sets LIBF_DELEXP on its own.
  104.  *
  105.  *    Exec has Forbid() for us during the call.
  106.  */
  107.  
  108. __geta4 __stkargs Library *
  109. LibClose(dummy, lib)
  110. long dummy;
  111. Library *lib;
  112. {
  113.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  114.     return(NULL);
  115.     if (lib->lib_Flags & LIBF_DELEXP)
  116.     return(LibExpunge(0, lib));
  117.     return(NULL);
  118. }
  119.  
  120. /*
  121.  *    We expunge the library and return the Seglist ONLY if the open count
  122.  *    is zero.    If the open count is not zero we set the DELAYED-EXPUNGE
  123.  *    flag and return NULL.
  124.  *
  125.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  126.  *    might be called from the memory allocator and thus we CANNOT DO A
  127.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  128.  *
  129.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  130.  *    therefore freeze if we called it ourselves.  As far as I can tell
  131.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  132.  *    below.
  133.  */
  134.  
  135. __geta4 __stkargs long
  136. LibExpunge(dummy, lib)
  137. long dummy;
  138. Library *lib;
  139. {
  140.     if (lib->lib_OpenCnt) {
  141.     lib->lib_Flags |= LIBF_DELEXP;
  142.     return(NULL);
  143.     }
  144.     Remove((MaxNode *)lib);
  145.     FreeMem((char *)lib-lib->lib_NegSize, lib->lib_NegSize+lib->lib_PosSize);
  146.  
  147. #ifdef NOTDEF
  148.     if (Iot0.tr_node.io_Device) {
  149.     CloseDevice(&Iot0);
  150.     Iot0.tr_node.io_Device = NULL;
  151.     }
  152. #endif
  153.  
  154.     return(SegList);
  155. }
  156.  
  157.